home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / SK (Sockects) 1.4.1 r2 / SK v1.4.1 r2.sit / SK 1.4.1 r2 / SK / SK Sources / SK_Utilities.c < prev    next >
Text File  |  1994-06-13  |  7KB  |  271 lines

  1.  
  2. /***************************************************************************
  3. * Copyright ⌐ 1992-1994 Matthias Neeracher and the Decision Systems Group
  4. * Permission is granted to anyone to use this software for any purpose on
  5. * any computer system, and to redistribute it freely, subject to the
  6. * following restrictions:
  7. * 1) The authors and the Decision Systems Group are not responsible for 
  8. *    the direct or indirect consequences of use of this software, no matter 
  9. *    how awful, even if they arise from defects in the software itself.
  10. *    This restriction applies to the use of this and any derived source code 
  11. *    and also to the use of all binary produced from this and any derived 
  12. *    source code.
  13. * 2) The origin and copyrights of this software must not be misrepresented, 
  14. *    either by explicit claim or by omission or alteration of copyright or
  15. *    authorship header information in this file or in any derived file.
  16. * 3) Altered or derived versions must be plainly marked as such, and must not
  17. *    be misrepresented as being the original software.
  18. * We encourage users of this software to provide feedback, bug fixes,
  19. * and enhancements to the authors for incorporation into future releases.
  20. *
  21. * ==========================================================================
  22. * FILE: CK_Utilities.c
  23. * AUTHOR: 
  24. *
  25. * Stephan R.A. Deibel
  26. * CREATION DATE: 
  27. * 5/26/94
  28. *
  29. * VERSION: 
  30. * 5/26/94
  31. *
  32. * DESCRIPTION: 
  33. * SK utilities implementations.  Includes file and directory management 
  34. * utilities needed for dealing with the /etc/services file and also
  35. * some basic tools for converting between C and Pascal strings.
  36. * NOTES: 
  37. * 0) The file management code is currently Mac-specific.  It could eventually
  38. *    be generalized, combined with other capabilities, and moved to the
  39. *    DSG's CU_* set of tools.
  40. *
  41. * 1) These functions were ported from Pascal code in the DSG's DeSyGNER/Explorer
  42. *    project.  Additional related code, including code for tracking files
  43. *    (a nearly platform-independent alias manager), can be found there.
  44. * MODIFICATIONS: 
  45. * --------------------------------------------------------------------------
  46. * Date     Name      Description of modification
  47. * --------------------------------------------------------------------------
  48. * 5/26/94  Stephan   Created from ported Pascal code.
  49. *
  50. * $Log$
  51. */
  52.  
  53. #include <Files.h>
  54. #include <string.h>
  55.  
  56. #include "SK_Utilities_P.h"
  57.  
  58.  
  59. /****************************************************************************
  60. * FUNCTION:
  61. *
  62. * SK_CtoPstr
  63. *
  64. * DESCRIPTION:
  65. *
  66. * Convert a C string into a Pascal string in place.
  67. *
  68. * PARAMETERS:
  69. *
  70. * char *    -- The C string to convert.
  71. *
  72. */
  73.  
  74. void SK_CtoPstr(char *str)
  75. {
  76.   short int len;
  77.   
  78.   len = strlen(str);
  79.   /* ASSERT(len <= 255); */
  80.   memcpy(&str[1], &str[0], len);
  81.   str[0] = (char) len;
  82. }
  83.  
  84.  
  85. /****************************************************************************
  86. * FUNCTION:
  87. *
  88. * SK_PtoCstr
  89. *
  90. * DESCRIPTION:
  91. *
  92. * Convert a pascal string into a C string in place.
  93. *
  94. * PARAMETERS:
  95. *
  96. * char *    -- The pascal string to convert.
  97. *
  98. */
  99.  
  100. void SK_PtoCstr(char *str)
  101. {
  102.   short int len;
  103.   
  104.   len = (short int) str[0];
  105.   memcpy(&str[0], &str[1], len);
  106.   str[len] = '\0';
  107. }
  108.  
  109.  
  110. #ifdef macintosh
  111. /****************************************************************************
  112. * FUNCTION:
  113. *
  114. * SK_FileRefToFullPath
  115. *
  116. * DESCRIPTION:
  117. *
  118. * Convert a Macintosh file vrefnum, directory ID, and file name into a
  119. * full path name.
  120. *
  121. * PARAMETERS:
  122. *
  123. * int        -- The vrefnum
  124. * long        -- The directory ID
  125. * Str255    -- The file name (pascal string)
  126. * Str255    -- Buffer space for the returned full path (pascal string)
  127. *
  128. */
  129.  
  130. OSErr 
  131. SK_FileRefToFullPath(int vrefnum, long dirID, Str255 filename, Str255 fullpath)
  132. {
  133.    FSSpec an_fsspec;
  134.    
  135.    if (FSMakeFSSpec(vrefnum, dirID, filename, &an_fsspec) == noErr)
  136.      return SK_FSSpecToFullPath(&an_fsspec, fullpath);
  137.    else {
  138.      *fullpath = '\0';
  139.      return noErr;
  140.    }
  141.  }
  142.  
  143.  
  144. /****************************************************************************
  145. * FUNCTION:
  146. *
  147. * SK_FileRefToFullPath
  148. *
  149. * DESCRIPTION:
  150. *
  151. * Convert a Macintosh FSSpec into a full path name.
  152. *
  153. * PARAMETERS:
  154. *
  155. * FSSpec *    -- The file specification
  156. * Str255    -- Buffer space for the returned full path (pascal string)
  157. *
  158. * RETURNS:
  159. * OSErr        -- 0 on success; -1 on error
  160. *
  161. * NOTES:
  162. *
  163. * Very inefficient implementation -- need a Pascal concat() and should
  164. * rewrite this to use that instead of doing so much string conversion.
  165. *
  166. */
  167.  
  168. OSErr 
  169. SK_FSSpecToFullPath(FSSpec *file_spec, Str255 fullpath)
  170. {
  171.    OSErr err;
  172.    
  173.    err = SK_DirIDToFullPath(file_spec->vRefNum, file_spec->parID, fullpath);
  174.    if (err == noErr) {
  175.      SK_PtoCstr((char *) fullpath);
  176.      SK_PtoCstr((char *) file_spec->name);
  177.      
  178.      if (strlen((char *) fullpath) + strlen((char *) file_spec->name) <= 255) {
  179.        strcat((char *) fullpath, (char *) file_spec->name);
  180.        err = noErr;
  181.      } else
  182.        err = -1;
  183.      
  184.      SK_CtoPstr((char *) file_spec->name);
  185.      SK_CtoPstr((char *) fullpath);
  186.    }
  187.      
  188.    return noErr;
  189. }
  190.  
  191.  
  192. /****************************************************************************
  193. * FUNCTION:
  194. *
  195. * SK_DirIDToFullPath
  196. *
  197. * DESCRIPTION:
  198. *
  199. * Convert a Macintosh file vrefnum and directory ID into a full path 
  200. * directory name.
  201. *
  202. * PARAMETERS:
  203. *
  204. * int        -- The vrefnum
  205. * long        -- The directory ID
  206. * Str255    -- Buffer space for the returned full path (pascal string)
  207. *
  208. * RETURNS:
  209. *
  210. * OSErr        -- 0 on success; <> 0 (a Mac OSErr) if some error occurred.
  211. *
  212. * NOTES:
  213. *
  214. * Very inefficient implementation -- need a Pascal concat() and should
  215. * rewrite this to use that instead of doing so much string conversion.
  216. *
  217. */
  218.  
  219. OSErr SK_DirIDToFullPath(int vrefnum, long dirID, Str255 fullpath)
  220. {
  221.   CInfoPBRec block;
  222.   Str255 directory_name = "\p";
  223.   Str255 ret_name = "\p";
  224.   OSErr err;
  225.  
  226.   block.dirInfo.ioNamePtr = directory_name;
  227.   block.dirInfo.ioDrParID = dirID;
  228.  
  229.   do {
  230.     block.dirInfo.ioVRefNum = vrefnum;
  231.     block.dirInfo.ioFDirIndex = -1;
  232.     block.dirInfo.ioDrDirID = block.dirInfo.ioDrParID;
  233.     
  234.     err = PBGetCatInfo(&block, FALSE);
  235.     if (err == noErr) {
  236.       SK_PtoCstr((char *) directory_name);
  237.       SK_PtoCstr((char *) ret_name);
  238.       strcat((char *) directory_name, SKk_FileSeperator);
  239.       strcat((char *) ret_name, (char *) directory_name);
  240.       SK_CtoPstr((char *) directory_name);
  241.       SK_CtoPstr((char *) ret_name);
  242.     }
  243.   } while ((block.dirInfo.ioDrDirID != 2) && (err == noErr));
  244.  
  245.   if (err != noErr) 
  246.     ret_name[0] = '\0';
  247.     
  248.   return err;
  249. }
  250. #endif /* macintosh */
  251.  
  252.  
  253. /* end of SK_Utilities.c */
  254.